package com.applink.sell_proof;

import android.app.Application;
import android.content.IntentFilter;
import android.net.ConnectivityManager;

import com.applink.sell_proof.broadcast_receiver.NetworkConnectivityReceiver;
import com.applink.sell_proof.constant.Constant;
import com.applink.sell_proof.database.table.CustomerSupportHelper;
import com.applink.sell_proof.util.Utility;

public class EMarketing extends Application {

	private volatile static EMarketing mUniqueInstance;

	private volatile static boolean mIsOnline = false;

	private String mAccountToken;
	private String mCoctactID;
	private String mAccountEmail;
	private String mAccountPassword;
	private String mAccountId;
	private String mExpired_server;
	private String mExpired_local;
	private String mAccessCode;

	public EMarketing() {
		super();
		mUniqueInstance = this;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		registerReceiver(new NetworkConnectivityReceiver(), new IntentFilter(
				ConnectivityManager.CONNECTIVITY_ACTION));
		Utility.checkConnect(this);
	}

	public static EMarketing getInstance() {
		return mUniqueInstance;
	}

	public void setAccountToken(String token) {
		mAccountToken = token;
		CustomerSupportHelper.storeString(Constant.ACCOUNT_TOKEN, token);
	}

	public String getJsonAccountToken() {
		if (mAccountToken == null)
			mAccountToken = CustomerSupportHelper
					.getString(Constant.ACCOUNT_TOKEN);
		return String.format("{\"token\":\"%s\"}", mAccountToken);
	}

	public String getAccountToken() {
		if (mAccountToken == null)
			mAccountToken = CustomerSupportHelper
					.getString(Constant.ACCOUNT_TOKEN);
		return mAccountToken;
	}

	public String getContactID() {
		if (mCoctactID == null)
			mCoctactID = CustomerSupportHelper.getString(Constant.CONTACT_ID);
		return mCoctactID;
	}

	public void setAccountEmail(String email) {
		mAccountEmail = email;
		CustomerSupportHelper.storeString(Constant.ACCOUNT_EMAIL, email);
	}

	public String getAccountEmail() {
		if (mAccountEmail == null)
			mAccountEmail = CustomerSupportHelper
					.getString(Constant.ACCOUNT_EMAIL);
		return mAccountEmail;
	}

	public void setAccountPassword(String password) {
		mAccountPassword = password;
		CustomerSupportHelper.storeString(Constant.ACCOUNT_PASSWORD, password);
	}

	public void setContactID(String contactID) {
		mCoctactID = contactID;
		CustomerSupportHelper.storeString(Constant.CONTACT_ID, contactID);
	}

	public String getAccountPassword() {
		if (mAccountPassword == null)
			mAccountPassword = CustomerSupportHelper
					.getString(Constant.ACCOUNT_PASSWORD);
		return mAccountPassword;
	}

	public void setAccountId(String accountId) {
		mAccountId = accountId;
		CustomerSupportHelper.storeString(Constant.ACCOUNT_ID, accountId);
	}

	public String getAccountId() {
		if (mAccountId == null)
			mAccountId = CustomerSupportHelper.getString(Constant.ACCOUNT_ID);
		return mAccountId;
	}

	public void setAccessCode(String accessCode) {
		mAccessCode = accessCode;
		CustomerSupportHelper.storeString(Constant.ACCESS_CODE, accessCode);
	}

	public String getAccessCode() {
		if (mAccessCode == null)
			mAccessCode = CustomerSupportHelper.getString(Constant.ACCESS_CODE);
		return mAccessCode;
	}

	public boolean isLogin() {
		return CustomerSupportHelper.getBoolean(Constant.IS_LOGIN);
	}

	public void setLogin(boolean isLogin) {
		CustomerSupportHelper.storeBoolean(Constant.IS_LOGIN, isLogin);
	}

	public boolean getCheckLog() {
		return CustomerSupportHelper.getBoolean(Constant.IS_CHECK_LOG);
	}

	public void setCheckLog(boolean isCheckLog) {
		CustomerSupportHelper.storeBoolean(Constant.IS_CHECK_LOG, isCheckLog);
	}

	public boolean isInvitationCode() {
		return CustomerSupportHelper.getBoolean(Constant.INVITATION_CODE);
	}

	public void setInvitationCode(boolean invitation) {
		CustomerSupportHelper
				.storeBoolean(Constant.INVITATION_CODE, invitation);
	}

	public static synchronized void setOnline(boolean isOnline) {
		mIsOnline = isOnline;
	}

	public static boolean isOnline() {
		return mIsOnline;
	}

	public void setExpiredServer(String expiredServer) {
		mExpired_server = expiredServer;
		CustomerSupportHelper.storeString(Constant.token_ExpriedServer,
				expiredServer);
	}

	public String getExpiredServer() {
		if (mExpired_server == null)
			mExpired_server = CustomerSupportHelper
					.getString(Constant.token_ExpriedServer);
		return mExpired_server;
	}

	public void setExpiredLocal(String expiredLocal) {
		mExpired_local = expiredLocal;
		CustomerSupportHelper.storeString(Constant.token_ExpriedLocal,
				expiredLocal);
	}

	public String getExpiredLocal() {
		if (mExpired_local == null)
			mExpired_local = CustomerSupportHelper
					.getString(Constant.token_ExpriedLocal);
		return mExpired_local;
	}
}
